package com.chinawlx.wlxteacher.ui.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.utils.WLXConstant;
import com.chinawlx.wlxteacher.utils.WLXGreenDaoUtil;
import com.chinawlx.wlxteacher.utils.WLXLogUtil;
import com.chinawlx.wlxteacher.utils.WLXTeacherConstant;
import com.chinawlx.wlxteacher.wlx_student;
import com.chinawlx.wlxteacher.wlx_studentDao;
import com.chinawlx.wlxteacher.wlx_student_order_detail;
import com.chinawlx.wlxteacher.wlx_student_order_detailDao;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wlx on 16/8/30.
 */
public class WLXSearchActivity extends WLXBaseActivity {
    @BindView(R.id.et_student_search)
    EditText mEditSearch;
    @BindView(R.id.lv_student_search)
    ListView mListView;
    @BindView(R.id.tv_cancel_search)
    TextView cancel;
    //    private List<wlx_student> wlxStudentsAll;
    private List<wlx_student> wlxStudentsAll;
    private StudentAdapter studentAllAdapter;
    private List<wlx_student_order_detail> orderDetails;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        initData();
        initView();
    }

    private void initView() {
        mEditSearch.setFocusable(false);
        studentAllAdapter = new StudentAdapter(wlxStudentsAll);
        mListView.setAdapter(studentAllAdapter);
        mEditSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                WLXLogUtil.printE("订单大小:" + orderDetails.size());
                if (TextUtils.isEmpty(s)) {
                    mListView.setAdapter(studentAllAdapter);
                    studentAllAdapter.notifyDataSetChanged();
                } else {
//                    List<wlx_student> searchList = new ArrayList<>();
//                    for (wlx_student_order_detail detail : orderDetails) {
//                        wlx_student student = WLXGreenDaoUtil.getStudentDao().queryBuilder()
//                                .where
//                                        (wlx_studentDao.Properties.Student_id.eq(detail
//                                                .getStudent_id()))
//                                .where
//                                        (wlx_studentDao.Properties
//                                        .Realname_pinyin.like("%" + s + "%"), wlx_studentDao
//                                        .Properties.Realname.like("%" + s + "%")).unique();
//                        WLXLogUtil.printE("student:" + student.getRealname());
                    List<wlx_student> searchList = WLXGreenDaoUtil.getStudentDao().queryBuilder()
                            .whereOr(wlx_studentDao.Properties
                                    .Realname_pinyin.like("%" + s + "%"), wlx_studentDao
                                    .Properties.Realname.like("%" + s + "%")).list();
//                        searchList.add(student);
//                    }
                    StudentAdapter studentSearchAdapter = new StudentAdapter(searchList);
                    mListView.setAdapter(studentSearchAdapter);
                    studentSearchAdapter.notifyDataSetChanged();
                }
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO: 16/9/29 搜索界面学生数据怎么筛选
                wlx_student_order_detail studentOrder = WLXGreenDaoUtil.getStudentOrderDetailDao()
                        .queryBuilder().where
                                (wlx_student_order_detailDao.Properties.Teacher_user_id.eq
                                        (WLXConstant
                                                .USER_ID)).where(wlx_student_order_detailDao
                                .Properties
                                .Student_id.eq(wlxStudentsAll.get(position).getStudent_id()))
                        .unique();
                String gradeId = Integer.toString(studentOrder.getGrade_id());
                String studentId = Integer.toString(studentOrder.getStudent_id());
//                String studentId = wlxStudentsAll.get(position).getStudent_id();
//                String gradeId = getArguments().getString(WLXTeacherConstant.GRADE_ID);
                Bundle bundle = new Bundle();
                bundle.putString(WLXTeacherConstant.GRADE_ID, gradeId);
                bundle.putString(WLXTeacherConstant.STUDENT_ID, studentId);
                WLXLogUtil.printE("班级id:" + gradeId + " 学生id:" + studentId);
                startActivity(WLXStudentDetailActivity.class, false, bundle);
            }
        });
    }

    private void initData() {
        wlxStudentsAll = new ArrayList<>();
        //当前登录教师的学生订单
        orderDetails = WLXGreenDaoUtil.getStudentOrderDetailDao()
                .queryBuilder().where(wlx_student_order_detailDao.Properties.Teacher_user_id.eq
                        (WLXConstant.USER_ID)).list();

        for (wlx_student_order_detail detail : orderDetails) {
            wlx_student student = WLXGreenDaoUtil.getStudentDao().queryBuilder().where
                    (wlx_studentDao.Properties
                            .Student_id.eq(detail.getStudent_id())).unique();
            wlxStudentsAll.add(student);
        }
//        wlxStudentsAll = WLXGreenDaoUtil.getStudentDao().loadAll();
    }

    @OnClick({R.id.et_student_search, R.id.tv_cancel_search})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.et_student_search:
                //这四个方法使点击edittext时使其获得焦点
                mEditSearch.setFocusable(true);
                mEditSearch.setFocusableInTouchMode(true);
                mEditSearch.requestFocus();
                mEditSearch.requestFocusFromTouch();
                break;
            case R.id.tv_cancel_search:
                finish();
                break;
        }
    }

    class StudentAdapter extends BaseAdapter {
        private List<wlx_student> list;

        public StudentAdapter(List<wlx_student> studentList) {
            this.list = studentList;
        }

        @Override
        public int getCount() {
            return list == null ? 0 : list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            StudentViewHolder holder = null;
            if (convertView == null) {
                holder = new StudentViewHolder();
                convertView = View.inflate(WLXSearchActivity.this, R.layout.item_stuent_search,
                        null);
                holder.icon = (ImageView) convertView.findViewById(R.id.iv_icon_student_search);
                holder.realName = (TextView) convertView.findViewById(R.id.tv_name_student_search);
                convertView.setTag(holder);
            } else {
                holder = (StudentViewHolder) convertView.getTag();
            }
            holder.realName.setText(list.get(position).getRealname());
            Glide.with(WLXSearchActivity.this).load(list.get(position).getAvatar_url
                    ()).into(holder.icon);
            return convertView;
        }
    }

    private static class StudentViewHolder {
        ImageView icon;
        TextView realName;
    }
}
